package com.cndw.kungfu.model.undercity;

import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
import com.cndw.kungfu.vo.PassThroughRecordVO;

public class RankingContainer extends ConcurrentSkipListSet<PassThroughRecordVO> {
	private static final long serialVersionUID = -4139371570421300281L;
	private int size;
	
	public RankingContainer(int size){
		this.size = size;
	}
	
	@Override
	public boolean add(PassThroughRecordVO e) {
		boolean add = false;
		if (size() < size || (last().compareTo(e) > 0)) {
			synchronized (this) {
				if (size() < size || (last().compareTo(e) > 0)) {
					add = super.add(e);
					if(add){
						Iterator<PassThroughRecordVO> ite = this.iterator();
						for(int i = 1, count = 0; ite.hasNext(); i++){
							PassThroughRecordVO p = ite.next();
							if(p.getPlayerId() == e.getPlayerId()){
								count++;
							}
							if(i > size || count > 1){
								ite.remove();
								i--;
								if(count > 1){
									count--;
								}
								continue;
							}
							p.setRanking(i);
						}
					}
				}
			}
		} 
		return add;
	}
}
